package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.SectionDao;
import cn.topevery.garden.entity.po.Section;
import cn.topevery.garden.entity.query.SectionQuery;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.DbScriptHelper;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author code-generate
 * @date 2021-08-06
 */
@Repository
public class SectionDaoImpl extends BaseDaoImpl<Section> implements SectionDao {
    @Override
    public <P extends DbCsp> List<Section> getSearch(P para) {
        return null;
    }


    @Override
    public DataPager<Section> listPage(SectionQuery query) {
        String table = "t_section ts";

        String columns = "ts.*";
        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("ts.c_id desc");
        sqlQueryMate
                .where(true, "ts.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotBlank(query.getName()), "ts.c_name", ScriptConditionEnum.Like, query.getName())
                .where(StringUtils.isNotBlank(query.getCode()), "ts.c_code", ScriptConditionEnum.Like, query.getCode())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, Section.class);
    }

    @Override
    public Map<String, Object> getBasicInfo(Integer id) {
        Map<String, Object> result = new HashMap<>();
        String sql = "t_section ts LEFT JOIN t_contract tc ON ts.c_id = tc.c_section_id  and tc.c_db_status=0 LEFT JOIN t_company tcom ON tc.c_company_id = tcom.c_id  and tcom.c_db_status=0 LEFT JOIN t_sys_dept tsd ON tc.c_manage_dept_id = tsd.c_id and tsd.c_db_status=0";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(sql).setColumns("( SELECT GROUP_CONCAT( tp.c_name ORDER BY tp.c_name SEPARATOR ',' ) FROM t_section_park_relation tspr JOIN t_park tp ON tspr.c_park_id = tp.c_id and tspr.c_db_status=0 and tp.c_db_status=0 WHERE tspr.c_section_id = ts.c_id ) parks, tc.c_manager_person AS managerContacts, tc.c_manager_person_phone AS managerContactsPhone, IFNULL(tsd.c_dept_name,'') AS managerName, tcom.c_name AS companyName, tcom.c_contacts AS companyContactsName, tcom.c_contacts_phone AS companyContactsPhone, tcom.c_address AS companyAddress, tc.c_contract_code AS contractCode, tc.c_name AS contractName,DATE_FORMAT(tc.c_bidding_date,'%Y-%m-%d') AS biddingDate, tc.c_bidding_money AS biddingMoney,DATE_FORMAT(tc.c_service_start_date,'%Y-%m-%d') AS serviceStartDate, DATE_FORMAT(tc.c_service_end_date,'%Y-%m-%d') AS serviceEndDate, tc.c_company_person_b AS companyPersonB");
        sqlQueryMate.where(true, "ts.c_db_status", ScriptConditionEnum.Eq, 0);
        sqlQueryMate.where(true, "ts.c_id", ScriptConditionEnum.Eq, id);
        return getOneMap(sqlQueryMate);
    }

    @Override
    public List<Map<String, Object>> getShortList() {
        String table = "t_section ts";
        List<String> columns = Arrays.asList("c_id as id", "c_name as name");

        SqlQueryMate sqlQueryMate = DbScriptHelper.getSelectSqlMate(table).setColumns(Strings.join(columns, ','));
        sqlQueryMate
                .where(true, "ts.c_db_status", ScriptConditionEnum.Eq, 0);
        ;
        return getMap(sqlQueryMate);
    }

    /**
     * 描述 通过名称获取Id.
     *
     * @param name the name
     * @return the integer
     * @author wenzhao.lin
     * @created 2021 -08-19 13:23:05 Get section by name integer.
     */
    @Override
    public Integer getSectionIdByName(String name) {
        String table = "t_section ts";

        String columns = "ts.*";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "ts.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, "ts.c_name", ScriptConditionEnum.Eq, name)
        ;
        List<Section> list = selectList(sqlQueryMate, Section.class);
        Integer tempId = null;
        if(list.size()>0){
            tempId = list.get(0).getId();
        }

        return tempId;
    }

}
